//
//  NSString.m
//  Meng800
//
//  Created by 姚宗超 on 14-7-23.
//  Copyright (c) 2014年 点财网. All rights reserved.
//

#import "NSDate+Business.h"

@implementation NSDate (Business)
// 将NSDate解析为聊天界面的时间戳标记
- (NSString *)parserToChatTimeStampString
{
//    return [[NSDate date] prettyTimestampSinceDate:date];
    return [[NSDate date] DCWTimestampSinceDate:self];
}

// private
- (NSString*)DCWTimestampSinceDate:(NSDate*)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSMinuteCalendarUnit | NSHourCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
    NSDate *earliest = [self earlierDate:date];
    NSDate *latest = (earliest == self) ? date : self;
    NSDateComponents *components = [calendar components:unitFlags fromDate:earliest toDate:latest options:0];
    if (components.day >= 1) {
        // 实例化一个NSDateFormatter对象
        NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
        //  设置Locale，星期几会以中文显示
        dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss a"];
        //  礼拜几,上下午等显示为英文          把AM,PM换成上午,下午显示为中文
        [dateFormat setAMSymbol:@"上午"];
        [dateFormat setPMSymbol:@"下午"];
        return [dateFormat stringFromDate:date];
    }
    if (components.hour >= 1) {
        return [self stringForComponentValue:components.hour withName:@"小时" andPlural:@"小时"];
    }
    if (components.minute >= 1) {
        return [self stringForComponentValue:components.minute withName:@"分钟" andPlural:@"分钟"];
    }
    return NSLocalizedString(@"刚刚", nil);
}

// private
- (NSString*)prettyTimestampSinceDate:(NSDate*)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSMinuteCalendarUnit | NSHourCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
    NSDate *earliest = [self earlierDate:date];
    NSDate *latest = (earliest == self) ? date : self;
    NSDateComponents *components = [calendar components:unitFlags fromDate:earliest toDate:latest options:0];
    
    if (components.year >= 1) {
        return NSLocalizedString(@"一年前", nil);
    }
    if (components.month >= 1) {
        return [self stringForComponentValue:components.month withName:@"个月" andPlural:@"months"];
    }
    if (components.week >= 1) {
        return [self stringForComponentValue:components.week withName:@"周" andPlural:@"周"];
    }
    if (components.day >= 1) {
        return [self stringForComponentValue:components.day withName:@"天" andPlural:@"天"];
    }
    if (components.hour >= 1) {
        return [self stringForComponentValue:components.hour withName:@"小时" andPlural:@"小时"];
    }
    if (components.minute >= 1) {
        return [self stringForComponentValue:components.minute withName:@"分钟" andPlural:@"分钟"];
    }
    return NSLocalizedString(@"刚刚", nil);
}

// private
- (NSString*)stringForComponentValue:(NSInteger)componentValue withName:(NSString*)name andPlural:(NSString*)plural
{
    NSString *timespan = NSLocalizedString(componentValue == 1 ? name : plural, nil);
    return [NSString stringWithFormat:@"%d%@%@", componentValue, timespan, NSLocalizedString(@"前", nil)];
}

// 将点财网服务器下发的时间格式转化为NSDate DD-MM-YYYY
- (NSString *)stringFromNSDate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat : @"dd-MM-yyyy"];
    return [formatter stringFromDate:self];
}

//YYYY-MM-DD
- (NSString *)stringFromNSDateDetail
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat : @"yyyy-MM-dd"];
    return [formatter stringFromDate:self];
}

// 2014-8-18 14:23
- (NSString *)detailStringFromDate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat : @"yyyy-MM-dd HH:mm"];
    return [formatter stringFromDate:self];
}

#pragma mark - Meng800
// Meng800用
- (NSString *)newsTableTimeString
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat : @"yyyy-MM-dd"];
    return [formatter stringFromDate:self];
}


// 打时间戳规定的事件间隔，超过此间隔，就要在聊天界面上显示时间戳
- (BOOL)earlierThanTimeInterval:(NSDate *)date
{
    NSTimeInterval interval = [self timeIntervalSinceDate:date];
    if (interval >= kChatTimeStampInterval) {
        return true;
    }
    return false;
}

#pragma mark Convenience methods

- (BOOL)isEarlierThanDate:(NSDate *)date
{
    return [self compare:date] == NSOrderedAscending;
}

- (BOOL)isEarlierThanOrEqualToDate:(NSDate *)date
{
    return [self compare:date] != NSOrderedDescending;
}

- (BOOL)isLaterThanDate:(NSDate *)date
{
    return [self compare:date] == NSOrderedDescending;
}

- (BOOL)isLaterThanOrEqualToDate:(NSDate *)date
{
    return [self compare:date] != NSOrderedAscending;
}

@end
